package org.cryptoutil;

import org.cryptoutil.CryptoUtil;

public class CryptoUtilExample {
	public static final String CLEAR_TEXT = "password";
	public static final String SEED = "myseed";
	
	public void testGenerateRandom() throws Exception {
		System.out.println("SecureRandom test");
		System.out.println("==================");
		System.out.println("Seed:" + SEED);
		System.out.println("SHA1PRNG:" + CryptoUtil.generateRandom("SHA1PRNG", 128, SEED, "UTF-8", null));
		System.out.println();
	}
	
	public void testMessageDigest() throws Exception {
		System.out.println("MessageDigest test");
		System.out.println("==================");
		System.out.println("Clear:" + CLEAR_TEXT);
		System.out.println("SHA1:" + CryptoUtil.digest(CLEAR_TEXT, "SHA-1", "UTF-8", null));
		System.out.println("MD5:" + CryptoUtil.digest(CLEAR_TEXT, "MD5", "UTF-8", null));
		System.out.println();
	}
	
	public void testAesEncryptDefault() throws Exception {
		System.out.println("AES encryption test");
		System.out.println("==================");
		System.out.println("Clear:" + CLEAR_TEXT);
		String key = "0123456789abcdef0123456789abcdef";
		System.out.println("Key:" + key);
		String cipher = CryptoUtil.aesEncryptUtf8(CLEAR_TEXT, key, null);
		System.out.println("Cipher:" + cipher);
		System.out.println();
	}
	
	public void testAesEncryptWithIV() throws Exception {
		System.out.println("AES encryption test (with IV)");
		System.out.println("============================");
		System.out.println("Clear:" + CLEAR_TEXT);
		String mode = "CBC";
		String padding = "PKCS5Padding";
		System.out.println("Mode:" + mode);
		System.out.println("Padding:" + padding);
		String key = "0123456789abcdef0123456789abcdef";
		String iv = "0123456789abcdef";
		System.out.println("Key:" + key);
		System.out.println("IV:" + iv);
		String cipher = CryptoUtil.aesEncryptUtf8(CLEAR_TEXT, key, iv, mode, padding, null);
		System.out.println("Cipher:" + cipher);
	}
	
	public static void main(String[] args) throws Exception {
		CryptoUtilExample example = new CryptoUtilExample();
		example.testGenerateRandom();
		example.testMessageDigest();
		example.testAesEncryptDefault();
		example.testAesEncryptWithIV();
	}
}